package JavaCodePublicClass.大文件的切割和合并;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class 大文件的切割和合并 {
    public static void main(String[] args) {
//        getSplitFile("C:\\video\\大文件的切割与合并\\林允儿.mp4", 3);
//        merge("", 3);
    }

    //    文件切割的具体方法
    public static long getWrite(String file, int index, long begin, long end) {
//        获取文件
        RandomAccessFile r = null;
        RandomAccessFile w = null;
        long endPointer = 0L;
        try {
//       读写文件
            r = new RandomAccessFile(new File(file), "r");
//        写出去的临时文件
            w = new RandomAccessFile(new File(file + " _" + index + ".tmp"), "rw");
//        用一个byte字节数
            byte[] b = new byte[1024];
//       指定开始位置
            r.seek(begin);
            int n = 0;
            //           切割
            while (r.getFilePointer() <= end && (n = r.read(b)) != -1) {
                w.write(b, 0, n);
            }
//            记录结束位置
            endPointer = r.getFilePointer();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (w != null) {
                    w.close();
                }
                if (r != null) {
                    r.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return endPointer;
    }

    public static void getSplitFile(String file, int count) {
        RandomAccessFile ref = null;
        try {
            ref = new RandomAccessFile(new File(file), "r");
            long length = ref.length();//获得了文件的字节数
//            计算每一份文件的大小
            long maxSize = length / count;
//            定义一个标记
            long offSet = 0L;
//            开始切割
            for (int i = 0; i < count; i++) {
//                起始位置
                long fBegin = offSet;
//                结束位置  切割到几分文件
                long fend = (i + 1) * maxSize;
//                切割
                offSet = getWrite(file, i, offSet, fend);
            }
//            最后一份文件
            if (length - offSet > 0) {
                getWrite(file, count - 1, offSet, length);
            }
//            System.out.println(length);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ref != null) {
                try {
                    ref.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //文件合并方法  file 合并的文件  count 合并的份数  void返回值
    public static void merge(String file, int count) {
        RandomAccessFile raf = null;
        RandomAccessFile reader = null;
        try {
            raf = new RandomAccessFile(new File(file), "rw");
            for (int i = 0; i < count; i++) {
                reader = new RandomAccessFile(new File(file + "_" + i + ".tmp"), "r");
                byte[] b = new byte[1024];
                int temp = 0;
                while ((temp = reader.read(b)) != -1) {
                    raf.write(b, 0, temp);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (raf != null) {
                    raf.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}